package com.google.code.ptrends;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.junit.Ignore;
import org.junit.Test;
import org.quartz.JobExecutionException;
import com.google.code.ptrends.common.ProcessorBuilder;
import com.google.code.ptrends.common.ProcessorBuilderImpl;
import com.google.code.ptrends.common.RetJob;
import com.google.code.ptrends.common.SupplierIdentifiers;
import com.google.code.ptrends.common.entities.Session;
import com.google.code.ptrends.common.utils.DerbyDB;
import com.google.code.ptrends.entities.utils.PrepareDBStrings;
import com.google.code.ptrends.locators.LocatorFactory;
import com.google.code.ptrends.locators.implementations.LocatorFactoryImpl;
import com.google.code.ptrends.services.DataService;
import com.google.code.ptrends.services.SessionService;
import com.google.code.ptrends.services.SupplierService;
import com.google.code.ptrends.services.implementations.DatabaseSessionService;
import com.google.code.ptrends.services.implementations.ETLService;
import com.google.code.ptrends.services.implementations.XmlSupplierService;
import com.google.code.ptrends.services.implementations.dataservice.DataServiceImpl;

public class ETLIntegrationCase {

	@Test
	@Ignore
	public void runETLCase() throws IOException, SQLException {
		DerbyDB database = new DerbyDB();

		database.executeSQL(PrepareDBStrings.generateAllTablesSQL());

		Connection connection = database.getConnection();
		runETLCaseImpl(connection);

		database.shutdown();
	}

	@Test
	@Ignore
	public void runRetJob() throws JobExecutionException {
		RetJob rj = new RetJob();
		rj.execute(null);
	}

	@Test
    //@Ignore
	public void runETLScenario_RealDB() throws ClassNotFoundException,
			SQLException {

		/*
		 * Please put ret.zip into ptrends\AllModules\ETL\src\test\resources
		 * before running this integration test
		 */

		Connection conn = getRealDatabaseConnection();

		runETLCaseImpl(conn);

		conn.close();
	}

	private void runETLCaseImpl(Connection connection) throws SQLException {
		DataService dataService = new DataServiceImpl(connection);
		SessionService sessionService = new DatabaseSessionService(dataService);

		Session session = sessionService.createSession(SupplierIdentifiers.RET);

		SupplierService supplierService = new XmlSupplierService();
		LocatorFactory locatorFactory = new LocatorFactoryImpl();
		ProcessorBuilder processorBuilder = new ProcessorBuilderImpl(session);

		ETLService service = new ETLService(locatorFactory, supplierService,
				processorBuilder, dataService);

		service.serve(SupplierIdentifiers.RET, session);

		sessionService.saveSession(session);
	}

	private Connection getRealDatabaseConnection()
			throws ClassNotFoundException, SQLException {
		Class.forName(REAL_DB_DRIVER);
		Connection conn = DriverManager.getConnection(REAL_DB_URL);
		return conn;
	}

	private static final String REAL_DB_URL = "jdbc:derby://localhost:1527/myDB;create=true";
	private static final String REAL_DB_DRIVER = "org.apache.derby.jdbc.ClientDriver";
}
